package com.neuedu.yyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.CommonResult;
import com.neuedu.yyzx.entity.Bed;
import com.neuedu.yyzx.entity.Checkin;
import com.neuedu.yyzx.entity.Worker;
import com.neuedu.yyzx.service.IBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 床位管理  前端控制器
 * </p>
 *
 * @author dya
 * @since 2021-04-27
 */
@RestController
@RequestMapping("//bed")
public class BedController {
    @Autowired
    IBedService bedService;

    @RequestMapping("/list")
    CommonResult list(Page page, Bed bed){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        if(bed!= null && StringUtils.isNotEmpty(bed.getCustomerName())){
            queryWrapper.like("customer_name",bed.getCustomerName());
        }
        return CommonResult.success(bedService.page(page,queryWrapper)) ;
    }


    @RequestMapping("/add")
    CommonResult add(Bed bed){
        return CommonResult.success( bedService.saveOrUpdate(bed));
    }


    @RequestMapping("/save")
    CommonResult save(Checkin checkin){
        Bed bed=new Bed(null,
                checkin.getRecordId(),
                checkin.getCustomerName(),
                checkin.getCustomerId(),
                checkin.getCustomerGender(),
                "有人",
                checkin.getCheckinDate(),
                checkin.getExpireDate(),
                checkin.getRoomId(),
                0
                );

        return  CommonResult.success(bedService.save(bed));

    }


    @RequestMapping("/getById")
    CommonResult getById(Integer bedId){

        return CommonResult.success(bedService.getById(bedId));
    }

    @RequestMapping("/del")
    CommonResult del(Integer bedId){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.set("is_deleted",1);
        updateWrapper.eq("bed_id",bedId);

        return CommonResult.success(bedService.update(updateWrapper));
    }


    @RequestMapping("/count")
    CommonResult count(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("count(bed_status) as count","bed_status");
        queryWrapper.groupBy("bed_status");
        queryWrapper.orderByDesc("bed_status");
        return CommonResult.success(bedService.listMaps(queryWrapper));
    }

    @RequestMapping("/floor")
    CommonResult  floor(Integer id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("SUBSTRING(bed_info,3,1)",id);
        return CommonResult.success(bedService.list(queryWrapper));
    }

    @RequestMapping("/data")
    CommonResult data(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        return CommonResult.success(bedService.list(queryWrapper)) ;

    }
}
